package com.openxt.nbzj.push;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.baidu.android.pushservice.PushManager;

import com.alibaba.fastjson.JSONArray;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016-07-26.
 */
public class Push {
    /**百度推送api_key*/
    public final static String PUSH_API_KEY = "xS5qHmhAdfrKkHF8jGXrEaiF";

    /**通知key*/
    public final static String PUSH_NOTIFIY_KEY = "$push_notifiy_key$";

    public final static String APP_NAME = "com.openxt.nbzj";

    public static final String PUSH_PREFS_NAME = "$push_data$";
    public static final String PUSH_CHANNELID = "$push_channelid$";

    public static void addPushTag(Context context, String tag) {
        try {
            if (tag == null)
                return;
            String tags = Utils.getLocalStorage(context, PUSH_PREFS_NAME);
            JSONArray $tags = null;
            if (StringUtil.isNull(tags)) {
                $tags = new JSONArray();
                $tags.add(tag);
            } else {
                $tags = JSON.parseArray(tags);
                if ($tags.contains(tag))
                    $tags.add(tag);
            }
            Utils.setLocalStorage(context, PUSH_PREFS_NAME, $tags.toString());
        } catch (Exception e) {

        }
    }

    public static void removePushTag(Context context, String tag) {
        try {
            if (tag == null)
                return;
            String tags = Utils.getLocalStorage(context, PUSH_PREFS_NAME);
            if (StringUtil.isNull(tags))
                return;
            JSONArray $tags = JSON.parseArray(tags);
            JSONArray _$tags = new JSONArray();
            for (int m = 0, n = $tags.size(); m < n; m++) {
                String _t = $tags.getString(m);
                if (_t.equals(tag))
                    continue;
                _$tags.add(_t);
            }
            if (_$tags.size() == 0)
                Utils.removeLocalStorage(context, PUSH_PREFS_NAME);
            else
                Utils.setLocalStorage(context, PUSH_PREFS_NAME, _$tags.toString());
        } catch (Exception e) {

        }
    }

    public static void clearPushTag(Context context) {
        Utils.removeLocalStorage(context, PUSH_PREFS_NAME);
    }

    public static void startPush(Context context) {
        //注册百度推送关联tag
        PushManager.listTags(context);
    }
}
